package org.jeecg.modules.zonglzp.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.zonglzp.entity.DepartmentEntity;
import org.jeecg.modules.zonglzp.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Department")
public class DepartmentController {

    @Autowired
    DepartmentService service;

    @PostMapping("/addDepartment")
    public JSONObject addDepartment(
            @RequestBody DepartmentEntity entity) {
        JSONObject obj = new JSONObject();
        //通过部门名字查询部门是否已经存在
        String departname = service.getdepartnamebyname(entity.getDepartName(),entity.getCustomerId());
        if (departname != null) {
            obj.put("code", 1);
            obj.put("data", "该部门名称已存在");
            return obj;
        }
        if (entity.getDepartName() == null || "".equals(entity.getDepartName()) || entity.getCustomerId() == null
                || "".equals(entity.getCustomerId())) {
            obj.put("code", 1);
            obj.put("data", "参数输入不完整");
            return obj;
        }
        int count = service.addDepartment(entity);

        if (count > 0) {
            obj.put("code", "0");
            obj.put("data", "新增成功");
        } else {
            obj.put("code", "1");
            obj.put("data", "新增失败");
        }

        return obj;
    }


    /*
    这里是查询所有的部门 以list返回
     */
    @GetMapping("/getAllDepartmentList")
    public JSONObject getAllDepartmentList(
            @RequestParam(name = "customerId") String customerId) {
        JSONObject obj = new JSONObject();
        List<DepartmentEntity> list = service.getAllDepartmentList(customerId);
        if (list != null && list.size() > 0) {
            obj.put("code", 0);
            obj.put("data", list);
        } else {
            obj.put("code", 1);
            obj.put("data", "没有数据");
        }
        return obj;
    }


    @GetMapping("/getDepartment")
    public JSONObject getDepartment(
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "customerId") String customerId
    ) {

        JSONObject obj = new JSONObject();
        Page<DepartmentEntity> page = new Page<DepartmentEntity>(pageNo, pageSize);
        IPage<DepartmentEntity> pageList = service.getDepartment(page, customerId);
        if (pageList != null && pageList.getRecords().size() > 0) {
            obj.put("code", 0);
            obj.put("data", pageList);
        } else {
            obj.put("code", 1);
            obj.put("data", "没有数据");
        }
        return obj;
    }


    @PostMapping("/deleteDepartment")
    public JSONObject deleteDepartment(
            @RequestParam(name = "id") int id
    ) {
        JSONObject obj = new JSONObject();

        int count = service.deleteDepartment(id);
        if (count > 0) {
            obj.put("code", "0");
            obj.put("data", "删除成功");
        } else {
            obj.put("code", "1");
            obj.put("data", "删除失败");
        }
        return obj;
    }

    @PostMapping("/updateDepartment")
    public JSONObject updateDepartment(
            DepartmentEntity entity
    ) {
        JSONObject obj = new JSONObject();

        //通过部门名字查询部门是否已经存在
        String departname = service.getdepartnamebyname(entity.getDepartName(),entity.getCustomerId());
        if (departname != null) {

            int oldid=service.getdepartIdbyname(entity.getDepartName(),entity.getCustomerId());

            if(oldid!=entity.getId()){
                obj.put("code", 1);
                obj.put("data", "该部门名称已存在");
                return obj;
            }


        }
        int count = service.updateDepartment(entity);
        if (count > 0) {
            obj.put("code", "0");
            obj.put("data", "修改成功");
        } else {
            obj.put("code", "1");
            obj.put("data", "修改失败");
        }
        return obj;
    }


    //编辑部门时候的详情
    @GetMapping("/getDepartmentbyId")
    public JSONObject getDepartmentbyId(
            @RequestParam(name = "id") int id
    ) {
        JSONObject obj = new JSONObject();
        DepartmentEntity department = service.getDepartmentbyId(id);
        if (department != null) {
            obj.put("code", "0");
            obj.put("data", department);
        } else {
            obj.put("code", "1");
            obj.put("data", "暂无数据");
        }
        return obj;
    }
}
